package com.scen.upload.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.scen.upload.domain.Notice;
import com.scen.upload.dto.NoticeDto;
import com.scen.upload.mapper.NoticeMapper;
import com.scen.upload.service.NoticeService;
import com.scen.upload.vo.DataGridView;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class NoticeServiceImpl implements NoticeService {

    @Autowired
    private NoticeMapper noticeMapper;

    /**
     * 分页查询
     *
     * @param noticeDto
     * @return
     */
    @Override
    public DataGridView listNoticePage(NoticeDto noticeDto) {
        Page<Notice> page = new Page<>(noticeDto.getPageNum(), noticeDto.getPageSize());
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(noticeDto.getNoticeTitle()), Notice.COL_NOTICE_TITLE, noticeDto.getNoticeTitle());
        queryWrapper.like(StringUtils.isNotBlank(noticeDto.getCreateBy()), Notice.COL_CREATE_BY, noticeDto.getCreateBy());
        queryWrapper.eq(StringUtils.isNotBlank(noticeDto.getNoticeType()), Notice.COL_NOTICE_TYPE, noticeDto.getNoticeType());
        queryWrapper.eq(StringUtils.isNotBlank(noticeDto.getStatus()), Notice.COL_STATUS, noticeDto.getStatus());
        queryWrapper.orderByDesc(Notice.COL_CREATE_TIME);
        this.noticeMapper.selectPage(page, queryWrapper);
        return new DataGridView(page.getTotal(), page.getRecords());
    }

    /**
     * 根据ID查询
     *
     * @param noticeId
     * @return
     */
    @Override
    public Notice getOne(Long noticeId) {
        return this.noticeMapper.selectById(noticeId);
    }

    /**
     * 添加
     *
     * @param noticeDto
     * @return
     */
    @Override
    public int addNotice(NoticeDto noticeDto) {
        Notice notice = new Notice();
        BeanUtil.copyProperties(noticeDto, notice);
        notice.setCreateTime(DateUtil.date());
        notice.setCreateBy(noticeDto.getSimpleUser().getUserName());
        return this.noticeMapper.insert(notice);
    }

    /**
     * 修改
     *
     * @param noticeDto
     * @return
     */
    @Override
    public int updateNotice(NoticeDto noticeDto) {
        Notice notice = new Notice();
        BeanUtil.copyProperties(noticeDto, notice);
        notice.setUpdateBy(noticeDto.getSimpleUser().getUserName());
        return this.noticeMapper.updateById(notice);
    }

    /**
     * 删除
     *
     * @param noticeIds
     * @return
     */
    @Override
    public int deleteNoticeByIds(Long[] noticeIds) {
        List<Long> ids = Arrays.asList(noticeIds);
        if (ids.size() > 0) {
            return this.noticeMapper.deleteBatchIds(ids);
        }
        return 0;
    }
}
